import { h, render } from 'vue'
import XtxConfirm from './xtx-confirm.vue'

const divVnode = h('div', { class: 'xtx-confirm-container' })
render(divVnode, document.body)
const container = divVnode.el

export const xtxConfirm = ({ title, text }) => {
  return new Promise((resolve, reject) => {
    // 单击确定
    const confirmCallback = () => {
      render(null, container)
      resolve()
    }

    // 点击取消
    const cancelCallback = () => {
      render(null, container)
      reject(new Error('点击取消'))
    }

    const ConfirmVNode = h(XtxConfirm, { title, text, confirmCallback, cancelCallback })
    render(ConfirmVNode, container)
  })
}

export default xtxConfirm
